package com.qingkai.onlineshop.presentation.detail

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.qingkai.onlineshop.R
import com.qingkai.onlineshop.data.entity.PicUrlEntity
import com.qingkai.onlineshop.presentation.dashboard.testItemWithPics

/**
 *作者：created by 89270 on 2025/6/13 18:14
 *描述：
 */
@Composable
fun ProductImages(
    modifier: Modifier = Modifier, images: List<PicUrlEntity>,
    count: Int = 0,
    onIncrease: () -> Unit,
    onDecrease: () -> Unit
) {
    val pager = rememberPagerState { images.size }
    Box(modifier = modifier) {
        HorizontalPager(modifier = Modifier.fillMaxSize(), state = pager) {
            val img = images[it]
            AsyncImage(
                model = img.url,
                contentDescription = img.id.toString(),
                modifier = Modifier.fillMaxSize(),
                error = painterResource(R.drawable.img_1_2),
                contentScale = ContentScale.Fit
            )
        }
        NumButton(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .height(30.dp)
                .width(120.dp)
                .offset(0.dp, 15.dp)
                .clip(CircleShape)
                .shadow(4.dp)
                .background(MaterialTheme.colorScheme.primary),
            count = count,
            onIncrease = onIncrease,
            onDecrease = onDecrease
        )
    }
}

@Composable
  fun NumButton(
    modifier: Modifier = Modifier,
    count: Int = 0,
    onIncrease: () -> Unit,
    onDecrease: () -> Unit
) {
    Row(
        modifier =modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Surface(
            modifier = Modifier
                .size(30.dp)
                .padding(2.dp)
                .clickable {
                    onDecrease()
                }, shape = CircleShape
        ) { Text(text = "-", modifier = Modifier.wrapContentSize(), textAlign = TextAlign.Center) }
        Text(
            text = "$count",
            textAlign = TextAlign.Center,
            modifier = Modifier.weight(1f),
            color = MaterialTheme.colorScheme.onPrimary
        )
        Surface(
            modifier = Modifier
                .size(30.dp)
                .padding(2.dp)
                .clickable {
                    onIncrease()
                }, shape = CircleShape
        ) { Text(text = "+", modifier = Modifier.wrapContentSize(), textAlign = TextAlign.Center) }

    }
}


@Preview
@Composable
fun ProductImagesPreview() {
    ProductImages(
        images = testItemWithPics.pics,
        count = 0, onIncrease = {}, onDecrease = {})
}
